unit basicio_data;

{$mode objfpc}{$H+}

{
  Stores all data of an IO-object
}

interface

uses
  Classes, SysUtils, basicio_map, basicio_player;

type
  TGameTurn = (GT_Wait,            //enemy turn, we have to wait
               GT_Build_City,      //we have to build a city
               GT_Build_Street,    //we have to build a street
               GT_Trade_Self,      //we are allowed to trade
               GT_Trade_Enemy,     //enemy is allowed to trade, we can accept
               GT_Build,           //we may build
               GT_Thief_ThrowRes,  //we have to throw res
               GT_Thief_Field,     //we have to move the thief
               GT_Thief_City);     //we have to decide from who we want to steal res

  { TIOData }
  TIOData = class
    private
      FDice: Integer;
      FMap: TMap;
      FPlayerList: TPlayerList;
      FTurn: TGameTurn;
    private
      function GetPlayer(AID: integer): TPlayer;
      function GetPlayerCount: integer;
    public
      constructor Create;
      destructor Destroy; override;
    public
      property Dice: integer read FDice write FDice;
      property Turn: TGameTurn read FTurn write FTurn;
      property Map: TMap read FMap;
      property Player[AID: integer]: TPlayer read GetPlayer;
      property PlayerCount: integer read GetPlayerCount;
    public
      function GetPlayerByColor(AColor: TPlayerColor): TPlayer;
      function GetPlayerIDByColor(AColor: TPlayerColor): Integer;
  end;

implementation

{ TIOData }

constructor TIOData.Create;
begin
  inherited;
  FMap := TMap.Create;
  FPlayerList := TPlayerList.Create;

  FPlayerList.Add(TPlayer.Create(PC_White));
  FPlayerList.Add(TPlayer.Create(PC_Yellow));
  FPlayerList.Add(TPlayer.Create(PC_Blue));
  FPlayerList.Add(TPlayer.Create(PC_Red));
end;

destructor TIOData.Destroy;
begin
  FPlayerList.Free;
  FMap.Free;
  inherited Destroy;
end;

function TIOData.GetPlayerByColor(AColor: TPlayerColor): TPlayer;
var
  i: integer;
begin
  result := nil;
  for i:= 0 to FPlayerList.Count-1 do
  begin
    if FPlayerList.Items[i].Color = AColor then
    begin
      result := FPlayerList.Items[i];
      exit;
    end;
  end;
end;

function TIOData.GetPlayerIDByColor(AColor: TPlayerColor): Integer;
var
  i: integer;
begin
  result := -1;
  for i:= 0 to FPlayerList.Count-1 do
  begin
    if FPlayerList.Items[i].Color = AColor then
    begin
      result := i;
      exit;
    end;
  end;
end;

function TIOData.GetPlayer(AID: integer): TPlayer;
begin
  result := FPlayerList.Items[AID];
end;

function TIOData.GetPlayerCount: integer;
begin
  result := FPlayerList.Count;
end;

end.

